package com.letv.work.source_logo;

import java.sql.Connection;
import java.text.MessageFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.letv.bean.SourceLogoBean;
import com.letv.bean.SummaryBean;
import com.letv.common.ConnectionManager;
import com.letv.conf.ConfigFactory;
import com.letv.mail.MailSenderInfo;
import com.letv.mms.cache.factory.CacheFactory;
import com.letv.util.DateUtil;
import com.letv.util.SQLUtil;
import com.letv.work.AbstarctWorkJob;

public class SourceLogoJob extends AbstarctWorkJob {

	private static final Logger log = LoggerFactory.getLogger(SourceLogoJob.class);
	private static String cbasekeyprefix = ConfigFactory.getString("cbaseprefix.name");
	private static String logoprefix = ConfigFactory.getString("sourceLogo.keyprefix");

	@Override
	public SummaryBean work(String type) {
		long startT = System.currentTimeMillis();
		String startTStr = DateUtil.getInstance().formatTime(startT);
		long stepT = System.currentTimeMillis();
		String stepTStr = DateUtil.getInstance().formatTime(stepT);

		SummaryBean summary = new SummaryBean();
		MailSenderInfo mailInfo = new MailSenderInfo();

		mailInfo.setSubject(MessageFormat.format(ConfigFactory.getString("mail.subject"), type));

		StringBuilder subject = new StringBuilder("新媒资离线索引模块，全量任务执行情况如下:<br><br>");
		StringBuilder detail = new StringBuilder("");

		subject.append("服务器IP:" + ConfigFactory.getString("mail.host") + "<br><br>");
		subject.append("开始时间:" + startTStr + "<br><br>");

		try {
			ConnectionManager connManager = ConnectionManager.getInstance();
			Connection conn = connManager.getConnectionFromLeso();
			detail.append("--------站点源Logo模块---------------------------<br><br>");
			List<SourceLogoBean> logoList = SQLUtil.getAllSourceLogos(conn);
			detail.append("获取站点源Logo数据：+ " + logoList.size() + "条<br><br>");
			int successCount = 0;
			try {
				for (SourceLogoBean logo : logoList) {

					String key = cbasekeyprefix + "_" + logoprefix + "_" + logo.getSourceName();
					CacheFactory.getCache().set(key, logo.getLogoUrl());
					successCount++;
				}
				String logoJson = JSON.toJSONString(logoList);
				String key = cbasekeyprefix + "_" + logoprefix;
				CacheFactory.getCache().set(key, logoJson);
			} catch (Exception e) {
				log.error("执行出错！", e);
			}

			detail.append("成功处理站点源Logo数据：+ " + successCount + "条<br><br>");
			detail.append("耗时:" + (System.currentTimeMillis() - stepT) / 1000 + "s<br><br>");
			stepT = System.currentTimeMillis();

			stepTStr = DateUtil.getInstance().formatTime(stepT);
			subject.append("结束时间:" + stepTStr + "<br><br>");
			subject.append(type + " 执行全量  总耗时:" + (stepT - startT) / 1000 + "s<br><br>");
			log.info(subject.toString() + detail.toString());
		} catch (Exception e) {
			stepT = System.currentTimeMillis();
			stepTStr = DateUtil.getInstance().formatTime(stepT);
			log.error("执行出错！", e);
			subject.append("结束时间:" + stepTStr + "<br><br>");
			subject.append(type + " 执行全量出错:" + e.getMessage() + "  总耗时:" + (stepT - startT) / 1000 + "s<br><br>");
		}
		// subject.append("====================================================="+"<br><br>");
		mailInfo.setContent(subject.toString() + detail.toString());
		// send mail
		sendMail(mailInfo);

		log.info("send " + type + " mail done!");

		return summary;
	}

}
